/**
 * weapp-js
 * https://code.aliyun.com/jionglee/weapp_base.git
 * Copyright@ 2021 Motigo, Inc.
 * 
 */
// import methods from './methods.js';

// import request from '../request.js';
// import session from '../session.js';
// import ui from '../ui.js';
// import ad from '../ad.js';
// import payment from '../payment.js';
// import baseRequest from '../baseRequest.js';
// import theme from '../theme.js';
// import audio from '../audio.js';
// import preload from '../preload.js';
// import file from '../file.js';
// import recorder from '../recorder.js';
// import softlogin from '../softlogin.js';
// import log from '../log.js';


// const KEYS = [request, session, ad, ui, payment, audio, preload, file, recorder, softlogin, log];
const KEYS = [];
const INGORE_KEYS = ['request', 'audio', 'recorder'];

function hasCallback(args) {
    if (!args || typeof args !== 'object') return false;

    const callback = ['success', 'fail', 'complete'];
    for (const m of callback) {
        if (typeof args[m] === 'function') return true;
    }
    return false;
}

function _promisify(func) {
    if (typeof func !== 'function') return fn;
    return (args = {}) =>
        new Promise((resolve, reject) => {
            func(
                Object.assign(args, {
                    success: resolve,
                    fail: reject
                })
            );
        });
}

export function promisifyAll(target = {}) {
    // console.log('promisifyAll');
    // console.log(target);
    // console.log(target.__proto__);
    let keys = [];
    for (const key in target) {
        keys.push(key);
    }
    if (KEYS.some( key => Object.getPrototypeOf(target)===Object.getPrototypeOf(key))) {
        // console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(target)));
        keys = keys.concat(Object.getOwnPropertyNames(Object.getPrototypeOf(target)))
    }
    // console.log(keys);
    keys.forEach(key => {
        const fn = target[key];
        // console.log(fn);
        // console.log(typeof fn);
        if (typeof fn === 'function') {
            // console.log('function');
            // console.log(fn.name);
            // console.log(fn.name.startsWith('_'));
            // if (flag) {
            //     if (methods[target].includes(key)) {
            //         target[key] = args => {
            //             if (hasCallback(args)) {
            //                 fn(args);
            //             } else {
            //                 return _promisify(fn)(args);
            //             }
            //         };
            //     } else {
            //     }
            // } else {
            //     target[key] = args => {
            //         if (hasCallback(args)) {
            //             fn(args);
            //         } else {
            //             return _promisify(fn)(args);
            //         }
            //     };
            // }
            if (key.startsWith('_')) {
                // 内部方法，不需兼容promise写法
            } else {
                // target[key] = args => {
                //     if (hasCallback(args)) {
                //         fn(args);
                //     } else {
                //         return _promisify(fn)(args);
                //     }
                // };
                target[key] = (args) => {
                    if (hasCallback(args)) {
                        fn.call(target,args);
                    } else {
                        return _promisify(fn.bind(target))(args);
                    }
                };
            }
        } else if (typeof fn === 'object' && fn !== null) {
            // console.log('object');
            // console.log(fn);
            // if (methods[fn] && [methods][fn].length) {
            //     // 需要进行 promisify
            //     promisifyAll(fn, true);
            // }
            if (!INGORE_KEYS.includes(key)) {
                promisifyAll(fn);
            }
        }
    });
}

export const promisify = _promisify;
